package com.example.shw

import android.app.Service
import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator
import android.support.v7.app.AppCompatActivity
import android.view.GestureDetector
import android.view.MotionEvent
import android.widget.LinearLayout
import android.widget.Toast

class VibratorActivity : AppCompatActivity(){
    private lateinit var vibrator: Vibrator
    private lateinit var detector: GestureDetector
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        var linearLayout = LinearLayout(this)
        setContentView(linearLayout)
        vibrator = getSystemService(Service.VIBRATOR_SERVICE) as Vibrator
        detector = GestureDetector(this, object : GestureDetector.SimpleOnGestureListener(){
            override fun onLongPress(e: MotionEvent) {
                super.onLongPress(e)
                Toast.makeText(this@VibratorActivity,"手机震动",
                    Toast.LENGTH_SHORT).show()
                vibrator.vibrate(VibrationEffect.createOneShot(2000, 180))
            }
        })
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        return detector.onTouchEvent(event)
    }
}